$(function () {
    let markId = getUrlString('markid');
    let isApp = getUrlString('isApp');//1是在小程序打开
    let goodId = '',imgSrc = '';
    if (isApp === '1'){
        $('#yinBtn').hide();
    }
    $('.All span').click(function () {
        $('.All span').removeClass('active');
        $(this).addClass('active');
        if ($(this).html() === '全部'){
            drawPrize('PrizeList','')
        } else if ($(this).html() === '只看剩余'){
            drawPrize('Residue','');
        }else {
            $('#convertList').hide();
            $('#silverList').show();
        }
    });

    $('.wrapTab .tabLi').click(function () {
        // alert($(this).index());
        if (isApp === '1'){
            if ($(this).index() === 0){
                wx.miniProgram.navigateTo({
                    url: '/pages/activity/myCard/myCard'
                });
            }else if ($(this).index() === 1){
                window.location.href = urlView + 'cardCenter/cardRecord.html?type=0&markid='+markId;
            } else {
                window.location.href = urlView + 'cardCenter/IntegralRanking.html?type=0&markid='+markId;
            }
        } else {
            if ($(this).index() === 0){
                window.location.href = 'dancool://web='+urlView+'cardCenter/myCard.html?type=0'
            }else if ($(this).index() === 1){
                window.location.href = 'dancool://web='+ urlView + 'cardCenter/cardRecord.html?type=0';
            } else {
                window.location.href =  'dancool://web='+ urlView + 'cardCenter/IntegralRanking.html?type=0';
            }
        }
    });

    function myCardList (MyCards) {
        let data = {
            'markid':markId,
            'method':MyCards
        };
        getData('post','/niu/apiv4/niu_mycards_recordV2.php',data,function (data) {
            console.log(data);
        })
    }
    myCardList('MyCards');

    $('#closeAlert').click(function () {
        $('.alertIndex').hide();
    });

    let isajax = true;

    function drawPrize (PrizeList,goodsId) {
        let data = {
            'markid':markId,
            'method':PrizeList,
            'goodsId':goodsId
        };
        getData('post','/niu/apiv4/niu_draw_prize_userV2.php',data,function (data) {
            console.log(data);
            isajax = true;
            if (data.resultcode === '0'){

                if (PrizeList === 'PrizeList' || PrizeList === 'Residue'){
                    let append = '';
                    $(data.data).each(function () {
                        append += '<li>' ;
                        if (this.leftcount === '0'){
                            append += '            <img class="convertEnd" src="../../res/img/my/cardCenter/ydw.png" alt="">' ;
                        }else {
                            append += '                <div class="getSend">兑换</div>' +
                                ' <input type="hidden" value="'+this.id+'">' ;
                        }
                        append+= '            <div class="left">' +
                            '                <img src="'+this.image+'" alt="">' +
                            '            </div>' +
                            '            <div class="right">' ;
                        if (this.name === '羊肉粽'){
                            append+=   '                <div class="name">猪肉粽</div>' ;
                        } else if (this.name === '红烧排骨粽') {
                            append+=   '                <div class="name">蜜枣粽</div>' ;
                        }else {
                            append+=   '                <div class="name">'+this.name+'</div>' ;
                        }
                         append+=

                            '                <div class="count">剩余数量：'+this.leftcount+'</div>' +
                            '                <div class="needCardNum">' ;
                        if (this.shu_card !== '0'){
                            append += '                    <span>粽叶:'+this.shu_card+'</span>'
                        }

                        if (this.niu_card !== '0'){
                            append += '                    <span>糯米:'+this.niu_card+'</span>'
                        }

                        if (this.hu_card !== '0'){
                            append += '                    <span>粽绳:'+this.hu_card+'</span>'
                        }

                        if (this.tu_card !== '0'){
                            append += '                    <span>猪肉:'+this.tu_card+'</span>'
                        }

                        if (this.long_card !== '0'){
                            append += '                    <span>红豆:'+this.long_card+'</span>'
                        }

                        if (this.she_card !== '0'){
                            append += '                    <span>蛋黄:'+this.she_card+'</span>'
                        }

                        if (this.ma_card !== '0'){
                            append += '                    <span>栗子:'+this.ma_card+'</span>'
                        }

                        if (this.yang_card !== '0'){
                            append += '                    <span>蜜枣:'+this.yang_card+'</span>'
                        }

                        // if (this.hou_card !== '0'){
                        //     append += '                    <span>猴:'+this.hou_card+'</span>'
                        // }
                        //
                        // if (this.ji_card !== '0'){
                        //     append += '                    <span>鸡:'+this.ji_card+'</span>'
                        // }
                        //
                        // if (this.gou_card !== '0'){
                        //     append += '                    <span>狗:'+this.gou_card+'</span>'
                        // }
                        //
                        // if (this.zhu_card !== '0'){
                        //     append += '                    <span>猪:'+this.zhu_card+'</span>'
                        // }
                        append+=  '                </div>' +
                            '            </div>' +
                            '        </li>'
                    });
                    $('#silverList').hide();
                    $('#convertList').empty().append(append).show();
                    $('#convertList li .getSend').click(function () {
                        goodId = $(this).next('input').val();
                        imgSrc = $(this).nextAll('.left').find('img').attr('src');
                        drawPrize('QueDing',goodId)
                    })

                } else if (PrizeList === 'QueDing') {
                    $('.toysname').html(data.name);
                    $('.toyspic').attr('src',imgSrc);
                    $('#leftcount').html('剩余数量：<strong>'+data.leftcount+'</strong>');
                        let surplusAppend = '';
                        if (data.PrizeData.length !== 0){
                            for(let key in data.PrizeData){
                                surplusAppend += '<span>'+key+':'+data.PrizeData[key]+'</span>'
                            }
                        }

                        if (data.PrizeData.length === 0){
                            $('#surplusAppend').empty();
                            $('#surplusNum').html('仍需卡片：<strong>0</strong>');
                            $('#noqrBtn').hide();
                            $('#qrBtn').show().click(function () {
                                if (isajax){
                                    isajax = false;
                                    drawPrize('Exchange',goodId);
                                }

                            })
                        } else {
                            $('#surplusNum').html('仍需卡片');
                            $('#qrBtn').hide();
                            $('#noqrBtn').show().click(function () {
                                $('.alertIndex').hide();
                            });
                            $('#surplusAppend').empty().append(surplusAppend);
                        }
                        $('.alertIndex').show();
                    } else if (PrizeList === 'Exchange') {
                        laymsg(data.msg);
                        $('.alertIndex').hide();
                        drawPrize('PrizeList','')
                    }
            }else if (data.resultcode === '2'){
                laymsg(data.msg);
            }else {
                laymsg(data.msg);
            }


        })
    }
    drawPrize('Residue','');

    // let cardId = '';
    // function  silvercardList (method,cardID) {
    //     let data = {
    //         'markid':markId,
    //         'method':method,
    //         'cardID':cardID
    //     };
    //     getData('post','/niu/apiv4/niu_yinbi_exchange_card.php',data,function (data) {
    //         console.log(data);
    //         if (data.resultcode === '0'){
    //             if (method === 'exchange'){
    //                 laymsg(data.msg);
    //                 $('.alert').hide()
    //             } else {
    //                 let append = '';
    //                 $(data.data).each(function () {
    //                     append += '<li>' +
    //                         '            <div class="left">' +
    //                         '                <img src="'+this.image+'" alt="">' +
    //                         '            </div>' ;
    //                     if (this.limit_count === '0'){
    //                         append += '            <img class="convertEnd" src="../../res/img/my/cardCenter/ydw.png?v=1.0.1" alt="">' ;
    //                     }
    //                     append += '            <div class="right">' +
    //                         '                <div class="name">'+this.name_extra+'</div>' +
    //                         // '                <div class="getSendType">活动期间每位用户限兑换x张</div>' +
    //                         '                 <input type="hidden" class="cardId" value="'+this.card_id+'"> '+
    //                         '                 <input type="hidden" class="prize" value="'+this.prize+'"> ';
    //                     if (this.limit_count === '0'){
    //                         append+='                <div style="background: #E7E7E7" class="silverBtn">' ;
    //                     }else {
    //                         append+='                <div class="silverBtn conversionBtn">' ;
    //                     }
    //                      append+=   '                    <img src="../../res/img/common/silverCoin.png" alt="">' +
    //                         '                    <span><span class="silverNum">'+this.prize+'</span><span>兑换</span></span>' +
    //                         '                </div>' +
    //                         '            </div>' +
    //                         '        </li>'
    //                 });
    //                 $('#silverList').append(append);
    //                 $('#silverList li .conversionBtn').click(function () {
    //                     let name = $(this).prevAll('.name').html();
    //                     cardId = $(this).prevAll('.cardId').val();
    //                     let prize = $(this).prevAll('.prize').val();
    //                     $('.alert .info').html('您确定用'+prize+'银币兑换'+name+'卡片吗？');
    //                     $('.alert').show();
    //                 });
    //
    //             }
    //         } else {
    //             $('.alert').hide();
    //             laymsg(data.msg)
    //         }
    //
    //     })
    // }
    // silvercardList('0','');

    $('#cancel').click(function () {
        $('.alert').hide()
    });

    // $('#confirm').click(function () {
    //     silvercardList('exchange',cardId);
    // })
});
